#!/bin/sh
#
# APPR - Automated Profiling and Performance Regression - is an
# extensible performance profiling framework for OpenOffice.org.
# 
# Copyright (C) 2005 Intel Corporation
# 
# GNU Lesser General Public License Version 2.1
# ======================================================================
# 
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
# 
# ======================================================================
#
# APPR toolset main program

BASEDIR=${0%/*}
HOOKDIR=$BASEDIR/appr_hook.d

TYPE=$1; shift
PHASE=$1
TIME=$(date +%H:%M:%S:%N)

if [ ${#APPR_LOG_DIR} -eq 0 ]; then
	APPR_LOG_DIR=$(pwd)
fi
LOGFILE=$APPR_LOG_DIR/appr_hook.log-$RUNTAG-$ROUND-$APPR_HOOK_TARGET_PID

case $PHASE in
INIT|DONE|BEGIN|END)
	echo
	echo "----$TIME $TYPE $PHASE----"
	;;
EXIT)
	echo
	echo "----$TIME $TYPE $PHASE----"
	;;
KILL)
	echo
	echo "----$TIME $TYPE $PHASE----"
	;;
ATTACH)
	echo
	echo "----$TIME $TYPE $PHASE----"
	echo $TIME $TYPE PRE-STR >> $LOGFILE
	;;
*)
	echo $TIME $TYPE $PHASE >> $LOGFILE
	;;
esac

HOOKLIST="strace monitor vtune"

cd $HOOKDIR
for hook in $HOOKLIST; do
	hookcmd=./$hook.hook

	if [ ! -f $hookcmd ] || [ ! -x $hookcmd ]; then 
		continue
	fi

	HOOKNAME=$(echo $hook | tr [:lower:] [:upper:])
	HOOKENABLE=APPR_HOOK_ENABLE_$HOOKNAME
	HOOKENABLE=${!HOOKENABLE}

	if [ ${#HOOKENABLE} -ne 0 ]; then
		# echo "hook service $HOOKNAME is enabled"
		$hookcmd $TIME $TYPE $PHASE
	fi
done

